package com.unknowpupil.common.swagger;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.AuthorizationScope;
import springfox.documentation.service.Contact;
import springfox.documentation.service.SecurityReference;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.List;

import static com.google.common.collect.Lists.newArrayList;

/**
 * @Classname: Swagger2Config
 * @Author: <a> w10g </a>
 * @CreateDate: Create in 2019/12/19 11:49
 * @Description:  .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) 扫描通过ApiOperation注解的
 * @Version: 1.0
 */
@ConditionalOnProperty(prefix = "swagger2", name = "base-package")
@Configuration
@EnableSwagger2
public class Swagger2Config {
    @Bean
    public Swagger2Properties properties(){
        return    new Swagger2Properties();
    }

    @Bean
    public Docket api() {
        System.out.println(properties().toString());
        return new Docket(DocumentationType.SWAGGER_2).
                apiInfo(apiInfo())
                .useDefaultResponseMessages(properties().getEnabled())
                .groupName(properties().getApiTitle())
                .select()
                .apis(RequestHandlerSelectors.basePackage(properties().getBasePackage()))
                .paths(PathSelectors.regex("^(?!api).*$"))
                .build()
                .securitySchemes(securitySchemes())
                .securityContexts(securityContexts())
                ;
    }
    private List<ApiKey> securitySchemes() {
        return newArrayList(
                new ApiKey(HttpHeaders.AUTHORIZATION, HttpHeaders.AUTHORIZATION, "header"));
    }
    private List<SecurityContext> securityContexts() {
        return newArrayList(
                SecurityContext.builder()
                        .securityReferences(defaultAuth())
                        .forPaths(PathSelectors.regex("^(?!api).*$"))
                        .build()
        );
    }
    List<SecurityReference> defaultAuth() {
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        return newArrayList(
                new SecurityReference(HttpHeaders.AUTHORIZATION, authorizationScopes));
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(properties().getApiTitle())
                .description("©2019 Copyright. Powered By w10g.")
                .contact(new Contact("unknowpupil", "http://unknowpupil.cn", "unknowpupil@163.com"))
                .version(properties().getVersion())
                .build();
    }
}
